package edu.gdkm.dao;

import com.github.pagehelper.Page;
import edu.gdkm.pojo.Book;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * 图书接口
 */
@Repository
public interface BookMapper {
    @Select("SELECT * FROM book where book_status !='3' order by book_uploadtime DESC")
    @Results(id = "bookMap", value = {
            //id字段默认为false，表示不是主键
            //column表示数据库表字段，property表示实体类属性名。
            @Result(id = true, column = "book_id", property = "id"),
            @Result(column = "book_name", property = "name"),
            @Result(column = "book_isbn", property = "isbn"),
            @Result(column = "book_press", property = "press"),
            @Result(column = "book_author", property = "author"),
            @Result(column = "book_pagination", property = "pagination"),
            @Result(column = "book_price", property = "price"),
            @Result(column = "book_uploadtime", property = "uploadTime"),
            @Result(column = "book_status", property = "status"),
            @Result(column = "book_borrower", property = "borrower"),
            @Result(column = "book_borrowtime", property = "borrowTime"),
            @Result(column = "book_returntime", property = "returnTime")
    })
    Page<Book> selectNewBooks();

    @Select("SELECT * FROM book where book_id=#{id}")
    @ResultMap("bookMap")
        //根据id查询图书信息
    Book findById(String id);

    //编辑图书信息
    @Update({
            "<script> " +
                    "update book " +
                    "<trim prefix=\"set\" suffixOverrides=\",\">"+
                    "<if test = \"name != null\"> " +
                    "book_name=#{name}, " +
                    "</if> " +
                    "<if test = \"isbn != null\"> " +
                    "book_isbn=#{isbn}, " +
                    "</if> " +
                    "<if test = \"press != null\"> " +
                    "book_press=#{press}, " +
                    "</if> " +
                    "<if test = \"author != null\"> " +
                    "book_author=#{author}, " +
                    "</if> " +
                    "<if test = \"pagination != null\"> " +
                    "book_pagination=#{pagination}, " +
                    "</if> " +
                    "<if test = \"price != null\"> " +
                    "book_price=#{price}, " +
                    "</if> " +
                    "<if test = \"uploadTime != null\"> " +
                    "book_uploadtime=#{uploadTime}, " +
                    "</if> " +
                    "<if test = \"status != null\"> " +
                    "book_status=#{status}, " +
                    "</if> " +
                    "<if test = \"borrower != null\"> " +
                    "book_borrower=#{borrower}, " +
                    "</if> " +
                    "<if test = \"borrowTime != null\"> " +
                    "book_borrowtime=#{borrowTime}, " +
                    "</if> " +
                    "<if test = \"returnTime != null\"> " +
                    "book_returntime=#{returnTime}, " +
                    "</if> " +
                    "</trim>"+
                    "WHERE book_id=#{id}",
            "</script>"
    })
    Integer editBook(Book book);
    @Select({"<script>" +
            "SELECT * FROM book " +
            "where book_status !='3'" +
            "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_borrowtime" +
            "</script>"
    })
    @ResultMap("bookMap")
    //分页查询图书
    Page<Book> searchBooks(Book book);

    @Insert("insert into book(book_id,book_name,book_isbn,book_press,book_author,book_pagination,book_price,book_uploadtime,book_status,book_borrower,book_borrowtime,book_returntime)" +
            " values(#{id},#{name},#{isbn},#{press},#{author},#{pagination},#{price},#{uploadTime},#{status},#{borrower},#{borrowTime},#{returnTime}) ")
    //新增图书
    Integer addBook(Book book);
    @Select(
            {"<script>" +
                    "SELECT * FROM book " +
                    "where "+
                    "book_status ='1'"+
                    "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
                    "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
                    "or book_status ='2'"+
                    "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
                    "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
                    "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
                    "order by book_borrowtime" +
                    "</script>"})
    @ResultMap("bookMap")
    //查询借阅但未归还的图书和待归还确认的图书
    Page<Book> selectBorrowed(Book book);

    @Select({"<script>"  +
            "SELECT * FROM book " +
            "where book_borrower=#{borrower}" +
            "AND book_status in('1','2')"+
            "<if test=\"name != null\"> AND  book_name  like  CONCAT('%',#{name},'%')</if>" +
            "<if test=\"press != null\"> AND book_press like  CONCAT('%', #{press},'%') </if>" +
            "<if test=\"author != null\"> AND book_author like  CONCAT('%', #{author},'%')</if>" +
            "order by book_borrowtime" +
            "</script>"})
    @ResultMap("bookMap")
    //查询借阅但未归还的图书
    Page<Book> selectMyBorrowed(Book book);
}
